//
//  JWLookSDCtry.m
//  test
//
//  Created by 李彦宏 on 2018/1/24.
//  Copyright © 2018年 朱圳. All rights reserved.
// 新增报名费 

#import "JWLookSDCtry.h"
#import "StuUserCell.h"
#import "BRPickerView.h"
#import "SDUserModel.h"
#import "JWUserManager.h"
#import "YYModel.h"
#import "UITextField+Limit.h"
@interface JWLookSDCtry ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate,UITextFieldDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *list;
@property(nonatomic,strong)SDUserModel *model;
@property(nonatomic,strong)UITextField *TextFile;
@property (nonatomic, strong) UIButton *rightBtnitem;
@property (nonatomic, strong) UITextView *text;

@end

@implementation JWLookSDCtry
-(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
  _text = [[UITextView alloc]init];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.model=[JWUserManager SDme];
    
    //加载plist文件
    NSString *path = [[NSBundle mainBundle] pathForResource:@"JWAddU.plist" ofType:nil];
    //存进数组里
    self.list =[NSMutableArray arrayWithContentsOfFile:path];
    [self.view addSubview:self.tableView];
    //rightNavBtn
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
    [rightBtn setFrame:CGRectMake(0, 0, 44, 44)];
    [rightBtn setTitle:@"修改" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.rightBtnitem=rightBtn;
    rightBtn.titleLabel.font=[UIFont systemFontOfSize:16];
    [rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
}
-(void)rightBtnClick:(UIButton *)send{
    if([send.titleLabel.text isEqualToString:@"修改"]){
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        [send setTitle:@"保存" forState:UIControlStateNormal];
        send.titleLabel.font=[UIFont systemFontOfSize:20];
        [send setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }else{
        if (self.model.QQ==nil){
            self.model.QQ=@" ";
        }
        if (self.model.Winxin==nil){
            self.model.Winxin=@" ";
        }
        if (self.model.BirthDay==nil){
            self.model.BirthDay=@" ";
        }
        if (self.model.dadphone==nil){
            self.model.dadphone=@" ";
        }
        if (self.model.mumphone==nil){
            self.model.mumphone=@" ";
        }
        if (self.model.BuyCourse==nil){
            self.model.BuyCourse=@" ";
        }
        if (self.model.classmoney==nil){
            self.model.classmoney=@" ";
        }
        if (self.model.Baiduyun==nil){
            self.model.Baiduyun=@" ";
        }
        if (self.model.Vyan==nil){
            self.model.Vyan=@" ";
        }
        if (self.model.math==nil){
            self.model.math=@" ";
        }
        if (self.model.english==nil){
            self.model.english=@"  ";
        }
        
        if (self.model.address==nil){
            self.model.address=@" ";
        }
        
        if (self.text.text.length==0 && self.model.mailbook==nil){
            self.text.text=@" ";
        }
        if(self.model.mailbook.length>0 && self.text.text.length==0)
        {
            self.text.text=self.model.mailbook;
        }
        self.model.mailbook=self.text.text;
        NSDictionary *dict = @{
                               @"jwid":JWid,
                               @"sid" : self.model.sid,
                               @"jwpassword" : JWpassword,
                               @"phone":self.model.phone,
                               @"name" : self.model.name,
                               @"sex" : self.model.sex,
                               @"xieyi":self.model.xieyi,
                               @"college" : self.model.college,
                               @"jieshu" : self.model.jieshu,
                               @"goal" : self.model.goal,
                               @"qnumber" : self.model.QQ,
                               @"wnumber" : self.model.Winxin,
                               @"birthday" : self.model.BirthDay,
                               @"dadphone" : self.model.dadphone,
                               @"mumphone" : self.model.mumphone,
                               @"class":self.model.BuyCourse,
                        @"classmoney":self.model.classmoney,
                               @"bdcloud" : self.model.Baiduyun,
                               @"vnumber" : self.model.Vyan,
                               @"address":self.model.address,
                               @"math":self.model.math,
                               @"english":self.model.english,
                               @"mailbook":self.model.mailbook,
                               };
        [[AFNetworkTool shareManager]postJSONWithUrl:JWteacher_UpdateStudent parmas:dict successData:^(id json) {
       //     NSLog(@"--学生修改返回json--%@", json);
            [JWUserManager saveSD:self.model];
            [Utility showSuccess:@"信息修改成功"];
            [self.navigationController popViewControllerAnimated:YES];
        } failure:^(NSError *error) {
            NSLog(@"%@", error);
        }];
    }
    
    
}

#pragma mark -给每个cell中的textfield添加事件，只要值改变就调用此函数
-(void)changedTextField:(id)sender
{
    UITextField *tempTextField = (UITextField *)sender;
  //  NSLog(@"tag------%ld;值是---%@",tempTextField.tag,tempTextField.text);
    switch (tempTextField.tag) {
        case 0:
            self.model.name = tempTextField.text;
            break;
        case 1:
            [tempTextField setLimitTextLength:11];
            self.model.phone = tempTextField.text;
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            self.model.college = tempTextField.text;
            break;
        case 5:
            self.model.jieshu = tempTextField.text;
            break;
        case 6:
//            self.model.address = tempTextField.text;
            break;
        case 7:
//            self.model.math = tempTextField.text;
            break;
        case 8:
//            self.model.english = tempTextField.text;
            break;
        case 9:
            self.model.goal = tempTextField.text;
            break;
        case 10:
            self.model.QQ = tempTextField.text;;
            break;
        case 11:
            self.model.Winxin = tempTextField.text;
            break;
        case 12:
            self.model.BirthDay = tempTextField.text;
            break;
        case 13:
            [tempTextField setLimitTextLength:11];
            self.model.dadphone = tempTextField.text;
            break;
        case 14:
            [tempTextField setLimitTextLength:11];
            self.model.mumphone = tempTextField.text;
            break;
        case 15:
            self.model.BuyCourse = tempTextField.text;
            break;
        case 16:
            self.model.classmoney = tempTextField.text;
            break;
        case 17:
            self.model.Baiduyun = tempTextField.text;
            break;
        case 18:
            self.model.Vyan = tempTextField.text;
            break;
            
        default:
            break;
    }
}
#pragma -mark —————————————————tap响应事件————————————————————
-(void)tapGesture:( UITapGestureRecognizer *)sender{
     [self.view endEditing:YES];
    [BRStringPickerView showStringPickerWithTitle:@"性别" dataSource:@[@"男", @"女"] defaultSelValue:@"男" isAutoSelect:YES resultBlock:^(id selectValue) {
//        NSLog(@"%@", selectValue);
        if ([selectValue isEqualToString:@"男"]) {
            self.model.sex = @"2";
//            NSLog(@">>>>>>>>%@",self.model.sex);
        }else{
            self.model.sex = @"1";
        }
        [self.tableView reloadData];
    }];
}
-(void)tapGesture2:( UITapGestureRecognizer *)sender{
     [self.view endEditing:YES];
    [BRDatePickerView showDatePickerWithTitle:@"出生年月" dateType:UIDatePickerModeDate defaultSelValue:@"" minDateStr:@"" maxDateStr:@"" isAutoSelect:YES resultBlock:^(NSString *selectValue) {
        self.model.BirthDay = selectValue;
        [self.tableView reloadData];
    }];
}
-(void)tapGesture3:( UITapGestureRecognizer *)sender{
     [self.view endEditing:YES];
    [BRStringPickerView showStringPickerWithTitle:@"协议寄回" dataSource:@[@"是", @"否"] defaultSelValue:@"否" isAutoSelect:YES resultBlock:^(id selectValue) {
 //       NSLog(@"%@", selectValue);
        if ([selectValue isEqualToString:@"是"]) {
            self.model.xieyi = @"1";
  //          NSLog(@">>>>>>>>%@",self.model.xieyi);
        }else{
            self.model.xieyi = @"0";
        }
        [self.tableView reloadData];
    }];
}
-(void)tapImgAction:(UITapGestureRecognizer *)send{
    [self.view endEditing:YES];
    __weak typeof(self) weakslf = self;
    [BRStringPickerView showStringPickerWithTitle:@"届数" dataSource:@[@"18", @"19", @"20", @"21", @"22", @"23"] defaultSelValue:@"20" isAutoSelect:YES resultBlock:^(id selectValue) {
        self.model.jieshu = selectValue;
        [weakslf.tableView reloadData];
    }];
}
//tap5Action:
-(void)tap5Action:(UITapGestureRecognizer *)send{
    [self.view endEditing:YES];
    __weak typeof(self) weakslf = self;
    [BRStringPickerView showStringPickerWithTitle:@"数学" dataSource:@[@"数一", @"数二", @"数三", @"396", @"199", @"无"] defaultSelValue:@"数一" isAutoSelect:YES resultBlock:^(id selectValue) {
        self.model.math = selectValue;
        [weakslf.tableView reloadData];
    }];
}
//tap6Action
-(void)tap6Action:(UITapGestureRecognizer *)send{
    [self.view endEditing:YES];
    __weak typeof(self) weakslf = self;
    [BRStringPickerView showStringPickerWithTitle:@"英语" dataSource:@[@"英一", @"英二", @"日语", @"其他"] defaultSelValue:@"英一" isAutoSelect:YES resultBlock:^(id selectValue) {
        self.model.english = selectValue;
        [weakslf.tableView reloadData];
    }];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    return 44;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.list.count;
}
#pragma -mark ——————————————Cell内容—————————————————
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"stuUserCell";
    StuUserCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[StuUserCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.nameLabel.text = self.list[indexPath.row][@"name"];
    cell.tximg.image = [UIImage imageNamed:self.list[indexPath.row][@"img"]];
    cell.dataText.tag = indexPath.row;
    cell.dataText.delegate=self;
    cell.dataText.textAlignment=NSTextAlignmentLeft;
    if([self.rightBtnitem.titleLabel.text isEqualToString:@"修改"])
    {

        cell.dataText.enabled=NO;
        
    }else{
        cell.dataText.enabled=YES;

        [cell.dataText addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    }
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
    tap.numberOfTouchesRequired = 1; //手指数
    tap.numberOfTapsRequired = 1; //tap次数
    tap.delegate= self;
    
    UITapGestureRecognizer *tap2=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture2:)];
    tap2.numberOfTouchesRequired = 1; //手指数
    tap2.numberOfTapsRequired = 1; //tap次数
    tap2.delegate= self;
    
    UITapGestureRecognizer *tap3=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture3:)];
    tap3.numberOfTouchesRequired = 1; //手指数
    tap3.numberOfTapsRequired = 1; //tap次数
    tap3.delegate= self;
    
    UITapGestureRecognizer *tap4=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImgAction:)];
    tap4.numberOfTapsRequired=1;
    tap4.numberOfTouchesRequired=1;
    tap4.delegate=self;
    //数学
    UITapGestureRecognizer *tap5=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap5Action:)];
    tap5.numberOfTapsRequired=1;
    tap5.numberOfTouchesRequired=1;
    tap5.delegate=self;
    //英语
    UITapGestureRecognizer *tap6=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap6Action:)];
    tap6.numberOfTapsRequired=1;
    tap6.numberOfTouchesRequired=1;
    tap6.delegate=self;
    
    

    if (indexPath.row == 0) {
        cell.dataText.text = self.model.name;
    }
    if (indexPath.row == 1) {
        cell.dataText.text = self.model.phone;
    }
    if (indexPath.row == 2) {
        if ([self.model.sex isEqualToString:@"1"]) {
            cell.dataText.text = @"女";
        }else{
            cell.dataText.text = @"男";
        }
        [cell.dataText addGestureRecognizer:tap];
    }
    if(indexPath.row==3){
        if([self.model.xieyi isEqualToString:@"0"]){
            cell.dataText.text=@"否";
        }else{
            cell.dataText.text=@"是";
        }
        [cell.dataText addGestureRecognizer:tap3];
    }
    if (indexPath.row == 4) {
        
        cell.dataText.text = self.model.college;
    }
    if (indexPath.row == 5) {
        cell.dataText.text = self.model.jieshu;
        [cell.dataText addGestureRecognizer:tap4];
    }
    if (indexPath.row == 6) {
        cell.dataText.text = self.model.address;
    }
    if (indexPath.row == 7) {
        cell.dataText.text = self.model.math;
        [cell.dataText addGestureRecognizer:tap5];
    }
    if (indexPath.row == 8) {
        cell.dataText.text = self.model.english;
        [cell.dataText addGestureRecognizer:tap6];
    }
    
    if (indexPath.row == 9) {
        cell.dataText.text = self.model.goal;
    }
    if (indexPath.row == 10) {
        cell.dataText.text = self.model.QQ;
    }
    if (indexPath.row == 11) {
        cell.dataText.text = self.model.Winxin;
    }
    if (indexPath.row == 12) {
        cell.dataText.text = self.model.BirthDay;
        [cell.dataText addGestureRecognizer:tap2];
    }
    if (indexPath.row == 13) {
        cell.dataText.text = self.model.dadphone;
    }
    if (indexPath.row == 14) {
        cell.dataText.text = self.model.mumphone;
    }
    if (indexPath.row == 15) {
        cell.dataText.text = self.model.BuyCourse;
    }
    if (indexPath.row == 16) {
        cell.dataText.text = self.model.classmoney;
    }
    
    if (indexPath.row == 17) {
        cell.dataText.text = self.model.Baiduyun;
    }
    if (indexPath.row == 18) {
        cell.dataText.text = self.model.Vyan;
    }
    return cell;
}

/** 区foot视图 */
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footer=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 160)];
   
    UILabel *lb1=[[UILabel alloc]init];
    lb1.text=@"邮寄书籍";
    lb1.textAlignment=NSTextAlignmentCenter;
    
    [footer addSubview:lb1];
    [lb1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(footer);
        make.top.equalTo(footer.mas_top).offset(0);
        make.height.mas_equalTo(30);
    }];
    
    _text.text=self.model.mailbook;
    _text.layer.borderWidth = 1;
    _text.layer.borderColor = [UIColor orangeColor].CGColor;
    [footer addSubview:_text];
    [_text mas_makeConstraints:^(MASConstraintMaker *make) {
        [make.top.mas_equalTo(lb1.mas_bottom) setOffset:5];
        [make.left.mas_equalTo(footer.mas_left) setOffset:10];
        [make.right.mas_equalTo(footer.mas_right) setOffset:-10];
        make.size.height.mas_equalTo(80);
    }];
    if([self.rightBtnitem.titleLabel.text isEqualToString: @"修改"]){
        _text.userInteractionEnabled=NO;
    }else{
        _text.userInteractionEnabled=YES;
    }
    
    return footer;
}
/*******设置高度解决Button点击没效果*******/
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 160;
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)style:UITableViewStyleGrouped];
        //隐藏组与导航栏间距
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView registerClass:[StuUserCell class] forCellReuseIdentifier:@"stuUserCell"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    if([self.rightBtnitem.titleLabel.text isEqualToString: @"修改"]){
        if (indexPath.row == 1) {
            NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@", self.model.phone];
            UIWebView * callWebview = [[UIWebView alloc] init];
            [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
            [self.view addSubview:callWebview];
        }
        if (indexPath.row == 13) {
            if ([self.model.dadphone isEqualToString:@""]) {
                
            }else{
                NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@", self.model.dadphone];
                UIWebView * callWebview = [[UIWebView alloc] init];
                [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
                [self.view addSubview:callWebview];
            }
        }
        if (indexPath.row == 14) {
            if ([self.model.mumphone isEqualToString:@""]) {
                
            }else{
                NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@", self.model.mumphone];
                UIWebView * callWebview = [[UIWebView alloc] init];
                [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
                [self.view addSubview:callWebview];
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
